<template>
  <div class="v-manage">
    <div class="pay-box" @click="bindCard">工资卡绑定<img src="../../assets/img/arrow.png" class="arrow" onclick="return false"/></div>
    <div class="pay-box" @click="withPwd">修改提现密码<img src="../../assets/img/arrow.png" class="arrow" onclick="return false"/></div>

    <v-dialog v-model="modalVal" content="请先进行工资卡绑定" type="default" @sure="addCard"></v-dialog>

    <!--微信绑定手机号-->
    <v-phone v-model="showModal" @bindPhone="bindCustomerPhone"></v-phone>
  </div>
</template>

<script>
  import phone from "../../components/phone"
  import dialog from "../../components/dialog"
  import {Base64} from "js-base64"
  import {getSessionStore, setSessionStore, getUrlKey, setStore} from "../../config/util"
  import {baseUrl} from "../../config/env"
  import Cookies from 'js-cookie'

  export default {
    data() {
      return {
        modalVal: false,
        userSign: "",
        showModal: false
      }
    },
    created() {
      this.openid = getUrlKey("openid"); //微信openid
      this.userName = getUrlKey("userName"); //用户昵称
      this.userUrl = getUrlKey("userUrl"); //用户头像
      this.countentPhone = getUrlKey("countentPhone"); //手机框 0弹出1不弹出

      if (this.openid != "" && this.openid != "null" && this.openid != null) {
        setSessionStore("openid", Base64.encode(this.openid));
      }
      if (this.userName != "" && this.userName != "null" && this.userName != null) {
        this.userName = decodeURI(this.userName);
        setSessionStore("weixin_userName", this.userName);
      }
      if (this.userUrl != "" && this.userUrl != "null" && this.userUrl != null) {
        setSessionStore("weixin_userUrl", this.userUrl);
      }

      this.userSign = Base64.decode(getSessionStore("openid"));
      if (this.countentPhone == "0") {
        this.showModal = true;
      }
    },
    methods: {
      bindCustomerPhone(phone) {//绑定微信客户手机号
        this.$vux.loading.show({
          text: '加载中',
        })
        //绑定微信手机
        this.axios.get(baseUrl + "/wx_user/set_phone?sgin=" + this.userSign + "&phone=" + phone + "&userName=" + this.userName + "&userUrl=" + this.userUrl).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0 && resultObj.data == 1) {
          } else {
            this.showModal = true;
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      bindCard() {
        let weixin_withdraw_sign = localStorage.getItem("weixin_withdraw_sign"); //判断不同的用户
        if (Base64.decode(getSessionStore("openid"))) {
          setSessionStore("paycard_sign", "001177");
          setStore("weixin_withdraw_sign", getSessionStore("openid")); //验证不同用户登录后提现密码次数的问题
          if (getSessionStore("openid") != weixin_withdraw_sign) { //不是当前用户清除十分钟次数和cookie的时间
            localStorage.removeItem("sess_time_local");
            Cookies.set('sess_time_local', "", {expires: -1, path: ''});
          }

          //查询工资卡信息
          this.axios.get(baseUrl + "/userbank/select_bindbankcard?sgin=" + this.userSign).then((res) => {
            let resultObj = res.data;
            if (resultObj.code == 0) {
              if (resultObj.data != null) {
                var bankObj = resultObj.data;
                if (bankObj.bankNumber != "" && bankObj.bankNumber != null) {
                  var regPhone = bankObj.phone; //系统手机号
                  setSessionStore("reserved_phone", regPhone);
                  setSessionStore("bank_user_name", bankObj.userName);
                  setSessionStore("identity_no", bankObj.identityNo);
                  setSessionStore("card_no", bankObj.bankNumber);
                  setSessionStore("bank_addr", bankObj.openBankAddr);
                  setSessionStore("bank_name", bankObj.bankName);
                  this.$router.push("/changecard");
                } else {//驻场手动输入姓名和身份证
                  setSessionStore("bank_user_name", bankObj.userName);
                  setSessionStore("identity_no", bankObj.identityNo);
                  this.$router.push("/identywrite_asce");
                }
              } else {
                this.$router.push("/bindcard");
              }
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
            }
          });
        }
      },
      addCard() {//跳转绑定工资卡
        this.modalVal = false;
      },
      withPwd() {//修改提现密码
        let weixin_withdraw_sign = localStorage.getItem("weixin_withdraw_sign"); //判断不同的用户
        if (Base64.decode(getSessionStore("openid"))) {
          setSessionStore("paycard_sign", "001177");
          setStore("weixin_withdraw_sign", getSessionStore("openid")); //验证不同用户登录后提现密码次数的问题
          if (getSessionStore("openid") != weixin_withdraw_sign) { //不是当前用户清除十分钟次数和cookie的时间
            localStorage.removeItem("sess_time_local");
            Cookies.set('sess_time_local', "", {expires: -1, path: ''});
          }

          this.axios.get(baseUrl + "/userbank/select_bindbankcard?sgin=" + this.userSign).then((res) => {
            let resultObj = res.data;
            if (resultObj.code == 0) {
              if (resultObj.data != null) {
                var bankObj = resultObj.data;
                if (bankObj.bankNumber != "" && bankObj.bankNumber != null) {
                  var regPhone = bankObj.phone; //系统手机号
                  setSessionStore("reserved_phone", regPhone);
                  sessionStorage.removeItem("changecard_sign");
                  this.$router.push("/withdraw_pwd");
                } else {
                  this.modalVal = true;
                }
              } else {
                this.modalVal = true;
              }
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
            }
          });
        }
      }
    },
    components: {
      "v-dialog": dialog,
      "v-phone": phone
    }
  }
</script>

<style lang="stylus" scoped>
  .v-manage
    width: 100%
    height: 100%
    .pay-box
      display: flex
      align-items: center
      padding-left: 0.6rem
      height: 1.333rem
      font-size: 0.44rem
      color: rgba(68, 68, 68, 1)
      border-bottom: 1px solid rgba(230, 230, 230, 1)
      .arrow
        margin-left: auto
        margin-right: 0.333rem
        width: 0.213rem
        height: 0.373rem
</style>
